<template>
	<view ref="my_mdj_input">
		<slot></slot>
	</view>

</template>

<script setup>

// import {
// 	isWeixin,
// 	isMQQbrowser,
// 	isAndroid
// } from '@/easy-util-ts/browser.js';
import {isWeixin,isMQQbrowser,isAndroid} from '@easyproduce/easy-util-ts/browser.js';
import {onMounted, ref} from 'vue'
// const OBSuploadFile = require('@/easy-uni3/OBS/OBSUploadFile.js');

// import {
// 	getFeExportOssPsp
// } from '@/model'
onMounted(() => {
	createUploadImgEle()
})
const emit = defineEmits(['on-face'])
const my_mdj_input = ref()

// 动态创建 input 上传img元素 主要为了兼容 
/**  
 * uni.chooseImg在调用相册和拍照在不同的手机中显示不同 无法调用相机 等问题
 * 在各个机型都可以点击 file 调用相册 和 摄像头拍照 
	1. 在老版本的安卓中，必须加上capture，否则只能调用相册 
	2. 在IOS中 加了capture，就只能调用摄像头不能调用相册
	*/
const createUploadImgEle =()=>{
	console.log('动态创建 input ');
	let input = document.createElement('input');
	input.type = 'file';
	input.accept = 'video/*';
	input.capture = 'user';
	input.style.position = "absolute";
	input.style.left = '0';
	input.style.top = '0';
	input.style.width = '100%';
	input.style.height = '100%';
	input.style.overflow = "hidden";
	input.style.opacity = 0;
	input.onchange = (event) => {
		console.log('动态添加的input ', event.target.files);
		let files = event.target.files;
		if (!files.length) return;
		let size = files[0] &&  files[0].size;
		if (size / 1024 / 1024 > 10) {
			uni.showToast({
				title: '当前视频过大，请控制在2至3秒内',
				icon: 'none',
				duration: 2000 // duration || 
			})
			return;
		}
		
		files = Array.prototype.slice.call(files);
		uploadFilePromise(files)
	}
	console.log("使用了什么内核：", window.navigator.userAgent);
	try {
		if (isAndroid() && isWeixin() && !isMQQbrowser()) {
			console.log("强制添加摄像头调用功能");
			// input.capture = 'camera';
			input.capture = 'user';
		}
	} catch (e) {
		console.log("创建原生input元素报错 ：", e);
	}

	my_mdj_input.value.$el.appendChild(input);
}

const uploadFilePromise =(files)=> {
	uni.showLoading()
	return new Promise(async (resolve, reject) => {
		let a = uni.uploadFile({
			url: process.env.npm_lifecycle_event === 'build:staging' || process.env.NODE_ENV == 'development' || process.env.NODE_ENV == 'staging' ? '/core/api/oss/upload/single' :`/core/api/oss/upload/single`,
			name: 'file',
			file: files[0],
			formData: {
				folder: 'hr'
			},
			success: (res) => {
				const {result} =JSON.parse(res.data);
				let https = result.data.https;

				console.log('face-video =====', https)
				emit('on-face', https);
				
				setTimeout(() => {
					resolve(https)
				}, 1000)
			},
			fail:(err) => {
				console.log(err)
				uni.hideLoading()
				uni.showToast({
					title: `${err.name}${err.message}`,
					icon: 'none'
				})
			}
		});
	})

}



	
</script>

<style lang="less">

</style>
